package cn.ihelloworld.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.ihelloworld.bean.Article;
import cn.ihelloworld.bean.Catagory;
import cn.ihelloworld.dao.IArticleDao;
import cn.ihelloworld.dao.ICatagoryDao;
@Component
public class IteyeParseHTML {
	@Autowired
	private IArticleDao articleDao;
	@Autowired
	private ICatagoryDao catagoryDao;
	public void doWork(){
		try {
			Document doc = Jsoup.connect("http://vipshichg.iteye.com/blog/1983678").userAgent("Mozilla")
.get();
			Element head=doc.select(".blog_title").first();
			Article article=new Article();
			String title=head.select("h3").select("a").text();
			article.setTitle(title);
			String catagorys=head.select(".blog_categories").select("li").select("a").text();
			String[] cas=catagorys.split(" ");
			List<Catagory> list=catagoryDao.codeOrNameIn(cas);
			if(list.size()>0){
				article.setCatagory(list.get(0));
			}
			String tags=head.select(".news_tag").select("a").text();
			tags=tags.replaceAll(" ", ",");
			Element content=doc.select(".blog_content").first();
			String scontent=content.toString();
			
//			article.setTag(tag);
			System.out.println(title);
			System.out.println(catagorys);
			System.out.println(tags);
			System.out.println("---------------------------------");
			System.out.println(scontent);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public static void main(String[] args){
		IteyeParseHTML parse=new IteyeParseHTML();
		parse.doWork();
	}
}
